home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Draw / Sources / LineShp.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  9.8 KB  |  372 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                LineShp.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef BASESHP_H
  15. #include "BaseShp.h"
  16. #endif
  17.  
  18. #ifndef LINESHP_H
  19. #include "LineShp.h"
  20. #endif
  21.  
  22. #ifndef UTILS_H
  23. #include "Utils.h"
  24. #endif
  25.  
  26. #ifndef CONSTANT_H
  27. #include "Constant.h"
  28. #endif
  29.  
  30. #ifndef DRAWPART_H
  31. #include "DrawPart.h"
  32. #endif
  33.  
  34. #ifndef DRAWPART_H
  35. #include "DrawPart.h"
  36. #endif
  37.  
  38. #ifndef DRAWFRM_H
  39. #include "DrawFrm.h"
  40. #endif
  41.  
  42. #ifndef DRAWPRXY_H
  43. #include "DrawPrxy.h"
  44. #endif
  45.  
  46. #ifndef DRAWLINK_H
  47. #include "DrawLink.h"
  48. #endif
  49.  
  50. #ifndef DRAWCLIP_H
  51. #include "DrawClip.h"
  52. #endif
  53.  
  54. // ----- Part Layer -----
  55.  
  56. #ifndef FWFRMING_H
  57. #include "FWFrming.h"
  58. #endif
  59.  
  60. #ifndef FWUTIL_H
  61. #include "FWUtil.h"
  62. #endif
  63.  
  64. #ifndef FWSELECT_H
  65. #include "FWSelect.h"
  66. #endif
  67.  
  68. #ifndef FWITERS_H
  69. #include "FWIters.h"
  70. #endif
  71.  
  72. // ----- OS Layer -----
  73.  
  74. #ifndef FWEVENT_H
  75. #include "FWEvent.h"
  76. #endif
  77.  
  78. #ifndef FWRECSHP_H
  79. #include "FWRecShp.h"
  80. #endif
  81.  
  82. #ifndef FWTXTBOX_H
  83. #include "FWTxtBox.h"
  84. #endif
  85.  
  86. #ifndef FWLINSHP_H
  87. #include "FWLinShp.h"
  88. #endif
  89.  
  90. #ifndef FWOVLSHP_H
  91. #include "FWOvlShp.h"
  92. #endif
  93.  
  94. #ifndef FWRRCSHP_H
  95. #include "FWRRcShp.h"
  96. #endif
  97.  
  98. #ifndef FWODGEOM_H
  99. #include "FWODGeom.h"
  100. #endif
  101.  
  102. // ----- Foundation Includes -----
  103.  
  104. #ifndef FWSTREAM_H
  105. #include "FWStream.h"
  106. #endif
  107.  
  108. // ----- OpenDoc Includes -----
  109.  
  110. #ifndef SOM_ODTransform_xh
  111. #include <Trnsform.xh>
  112. #endif
  113.  
  114. //========================================================================================
  115. // Runtime Information
  116. //========================================================================================
  117.  
  118. #ifdef FW_BUILD_MAC
  119. #pragma segment odfdrawshapes
  120. #endif
  121.  
  122. //========================================================================================
  123. // RunTime Info
  124. //========================================================================================
  125.  
  126. FW_DEFINE_CLASS_M1(CLineShape, CBaseShape)
  127.  
  128. FW_REGISTER_ARCHIVABLE_CLASS(LLineShape, CLineShape, CLineShape::Read, CBaseShape::Write)
  129.  
  130. //========================================================================================
  131. // class CLineShape
  132. //========================================================================================
  133.  
  134. //----------------------------------------------------------------------------------------
  135. // CLineShape::CLineShape
  136. //----------------------------------------------------------------------------------------
  137.  
  138. CLineShape::CLineShape() :
  139.     CBaseShape(2, kLineShape, kFrameOnly)
  140. {
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // CLineShape::CLineShape
  145. //----------------------------------------------------------------------------------------
  146.  
  147. CLineShape::CLineShape(FW_CReadableStream& archive) :
  148.     CBaseShape(archive)
  149. {    
  150.     archive >> fStart;
  151.     archive >> fEnd;
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // CLineShape::~CLineShape
  156. //----------------------------------------------------------------------------------------
  157.  
  158. CLineShape::~CLineShape()
  159. {
  160. }
  161.  
  162. //----------------------------------------------------------------------------------------
  163. // CLineShape::RenderShape
  164. //----------------------------------------------------------------------------------------
  165.  
  166. void CLineShape::RenderShape(Environment* ev, ODFacet* facet, FW_CGraphicContext& gc)
  167. {    
  168.     FW_CLineShape::RenderLine(gc, fStart, fEnd, fFrameInk, fFrameStyle);        
  169. }
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // CLineShape::OutlineShape
  173. //----------------------------------------------------------------------------------------
  174.  
  175. void CLineShape::OutlineShape(FW_CGraphicContext& gc, const FW_PInk& ink, const FW_PStyle& style, 
  176.                               const FW_CPoint& pt1, const FW_CPoint& pt2)
  177. {
  178.     FW_CLineShape::RenderLine(gc, pt1, pt2, ink, style);        
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // CLineShape::HitTest
  183. //----------------------------------------------------------------------------------------
  184.  
  185. FW_Boolean CLineShape::HitTest(Environment *ev, FW_CGraphicContext& gc, const FW_CMouseEvent& theMouseEvent) const
  186. {
  187.     // We should convert the point to shape coordinate
  188.     FW_CPoint mouse = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  189.     ODFacet* facet = theMouseEvent.GetFacet(ev);
  190.     FW_CFrame* frame = FW_CFrame::ODtoFWFrame(ev, facet->GetFrame(ev));
  191.     frame->GetContentView(ev)->FrameToViewContent(ev, mouse);
  192.  
  193.     FW_CLineShape lineShape(fStart.x, fStart.y, fEnd.x, fEnd.y);
  194.     return lineShape.HitTest(gc, mouse, FW_IntToFixed(2));
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // CLineShape::GetClipRegion
  199. //----------------------------------------------------------------------------------------
  200.  
  201. void CLineShape::GetClipRegion(Environment* ev, ODShape* clipRegion)
  202. {
  203.     ODShape* lineShape = ::FW_CreateLineODShape(ev, fStart, fEnd, GetPenSize());
  204.     clipRegion->CopyFrom(ev, lineShape);
  205.     lineShape->Release(ev);
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. // CLineShape::CreateShapeOutline
  210. //----------------------------------------------------------------------------------------
  211.  
  212. ODShape* CLineShape::CreateShapeOutline(Environment *ev)
  213. {
  214.     ODShape* dragShape = ::FW_NewODShape(ev);        // We don't acquire it because we return it
  215.     GetClipRegion(ev, dragShape);
  216.     return dragShape;
  217. }
  218.  
  219. //----------------------------------------------------------------------------------------
  220. // CLineShape::GetHandleCenter
  221. //----------------------------------------------------------------------------------------
  222.  
  223. void CLineShape::GetHandleCenter(short whichHandle, FW_CPoint& center) const
  224. {
  225.     center = whichHandle == kInTopLeftCorner ? fStart : fEnd;
  226. }
  227.  
  228. //----------------------------------------------------------------------------------------
  229. // CLineShape::ResizeFeedback
  230. //----------------------------------------------------------------------------------------
  231.  
  232. void CLineShape::ResizeFeedback(FW_CGraphicContext& gc, const FW_PInk& ink, const FW_PStyle& style, 
  233.                                 short whichHandle, const FW_CPoint& mouseLoc)
  234. {
  235.     FW_CRect srcRect, dstRect;
  236.     GetMapRects(whichHandle, mouseLoc, srcRect, dstRect);
  237.     
  238.     FW_CPoint pt1(fStart);
  239.     FW_CPoint pt2(fEnd);
  240.  
  241.     pt1.Map(srcRect, dstRect);
  242.     pt2.Map(srcRect, dstRect);
  243.  
  244.     if (srcRect.left == srcRect.right)
  245.     {
  246.         pt1.x = dstRect.left;
  247.         pt2.x = dstRect.right;
  248.     }
  249.     
  250.     if (srcRect.top == srcRect.bottom)
  251.     {
  252.         pt1.y = dstRect.top;
  253.         pt2.y = dstRect.bottom;
  254.     }
  255.     
  256.     OutlineShape(gc, ink, style, pt1, pt2);
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. // CLineShape::GetMapRects
  261. //----------------------------------------------------------------------------------------
  262.  
  263. void CLineShape::GetMapRects(short whichHandle, const FW_CPoint& mouseLoc,
  264.                                 FW_CRect& srcRect, FW_CRect& dstRect)
  265. {
  266.     srcRect.Set(fStart.x, fStart.y, fEnd.x, fEnd.y);
  267.     dstRect = srcRect;
  268.  
  269.     switch (whichHandle)
  270.     {
  271.         case kInTopLeftCorner:
  272.             dstRect.left = mouseLoc.x;
  273.             dstRect.top = mouseLoc.y;
  274.             break;
  275.         case kInBottomRightCorner:
  276.             dstRect.right = mouseLoc.x;
  277.             dstRect.bottom = mouseLoc.y;
  278.             break;
  279.     }
  280. }
  281.  
  282. //----------------------------------------------------------------------------------------
  283. // CLineShape::MapShape
  284. //----------------------------------------------------------------------------------------
  285.  
  286. void CLineShape::MapShape(Environment *ev, CDrawPart* part, const FW_CRect& srcRect, const FW_CRect& dstRect)
  287. {
  288. FW_UNUSED(srcRect);
  289.  
  290.     CheckPromise(ev, part);
  291.     
  292.     ClearCache();
  293.  
  294.     FW_CRect realSrcRect;
  295.     realSrcRect[FW_kTopLeft] = fStart;
  296.     realSrcRect[FW_kBotRight] = fEnd;
  297.  
  298.     fStart.Map(realSrcRect, dstRect);
  299.     fEnd.Map(realSrcRect, dstRect);
  300.  
  301.     if (realSrcRect.left == realSrcRect.right)
  302.     {
  303.         fStart.x = dstRect.left;
  304.         fEnd.x = dstRect.right;
  305.     }
  306.     
  307.     if (realSrcRect.top == realSrcRect.bottom)
  308.     {
  309.         fStart.y = dstRect.top;
  310.         fEnd.y = dstRect.bottom;
  311.     }
  312. }
  313.  
  314. //----------------------------------------------------------------------------------------
  315. // CLineShape::OffsetShape
  316. //----------------------------------------------------------------------------------------
  317.  
  318. void CLineShape::OffsetShape(Environment *ev, FW_CFixed xDelta, FW_CFixed yDelta)
  319. {
  320.     ClearCache();
  321.     
  322.     FW_CPoint offset(xDelta, yDelta);
  323.  
  324.     FW_CAcquiredODTransform aqTransform = ::FW_NewODTransform(ev, offset);
  325.     
  326.     fStart.Transform(ev, aqTransform);
  327.     fEnd.Transform(ev, aqTransform);
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. // CLineShape::SetShapeGeometry
  332. //----------------------------------------------------------------------------------------
  333.  
  334. void CLineShape::SetShapeGeometry(const FW_CPoint& anchorPoint,  const FW_CPoint& currentPoint)
  335. {
  336.     fStart = anchorPoint;
  337.     fEnd = currentPoint;
  338. }
  339.  
  340. //----------------------------------------------------------------------------------------
  341. // CLineShape::Flatten
  342. //----------------------------------------------------------------------------------------
  343.  
  344. void CLineShape::Flatten(FW_CWritableStream& archive)
  345. {    
  346.     CBaseShape::Flatten(archive);
  347.     
  348.     archive << fStart;
  349.     archive << fEnd;
  350. }
  351.  
  352. //----------------------------------------------------------------------------------------
  353. // CLineShape::Read
  354. //----------------------------------------------------------------------------------------
  355.  
  356. void* CLineShape::Read(FW_CReadableStream& archive)
  357. {
  358.     return new CLineShape(archive);
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. // CLineShape::GetRectGeometry
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void CLineShape::GetRectGeometry(FW_CRect& rect) const
  366. {
  367.     rect[FW_kTopLeft] = fStart;
  368.     rect[FW_kBotRight] = fEnd;
  369.     rect.Sort();
  370. }
  371.  
  372.